package com.z;


import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Main {

    // 读入的流
    FileReader fileReader;
    BufferedReader bufferedReader;
    // 按行读入数据并进行处理
    String instruction;
    // 存储读入命令集合
    Queue<String> instructions;
    // 解析对象，对命令集合做处理
    Parser parser;
    // 最终输出的文件名
    String fileName;
    // 文件路径
    String filePath;



    Main(String filePath,String fileName){
        try {
            fileReader = new FileReader(filePath);
            bufferedReader = new BufferedReader(fileReader);
            instructions = new LinkedList<>();
            this.fileName = fileName;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 创建parser
    public void ToParse(){
        try {

            while((instruction = bufferedReader.readLine() )!= null){

                if (instruction.matches("^[\\n]+$")) {
                    continue;
                }

                if (instruction.contains("//")){
                    continue;
                }

                instructions.add(instruction);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        parser = new Parser(instructions,fileName);

    }



    public static void main(String[] args) throws IOException {

        Main main = new Main("C:\\Users\\13187\\nand2tetris\\nand2tetris\\projects\\07\\StackArithmetic\\SimpleAdd\\SimpleAdd.vm","SimpleAdd");
        main.ToParse();
        String str = main.parser.processFile();

        String outFileName = "C:\\Users\\13187\\Desktop\\" + "SimpleAdd.asm";
        // 创建输出流
        FileWriter fileWriter = new FileWriter(outFileName);

        fileWriter.write(str);

        fileWriter.close();

        System.out.println("输出完成");


    }
}
